package com.voxel.sense.infomsg.module.firm;

import android.text.TextUtils;

import com.example.com.common.factory.data.DataSource;
import com.voxel.sense.infomsg.base.RecyclerViewPresenter;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.utils.manager.QueryConfigManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.net.helper.FirmNetHelper;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.FirmListEntity;
import cyy.example.com.facotry.model.api.response.SearchMenuEntity;

/**
 * Created by ChenYiYao on 2018/2/8.
 */

public class FirmListInfoPresenter extends RecyclerViewPresenter<FirmListInfoContract.View>
        implements FirmListInfoContract.Presenter, DataSource.Callback<BaseEntity<List<FirmListEntity>>> {
    private Map<String, Object> mQueryMap;//选择添加的条件
    private int CURRENT_PAGE_SIZE = 0;//加载数据的页数

    public FirmListInfoPresenter(FirmListInfoContract.View view) {
        super(view);
    }


    @Override
    public void start() {
        super.start();
        mQueryMap = new HashMap<>();
        mQueryMap.put(Constants.TYPE_AREA, new ArrayList());
        mQueryMap.put(Constants.TYPE_IDUSTRY, new ArrayList());
        EventBus.getDefault().register(this);
    }

    @Override
    public void destroy() {
        super.destroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 转化数据
     */
    private List<ConditionBean> CoverMenuData(SearchMenuEntity entity, String type) {
        List<ConditionBean> datas = new ArrayList<>();
        if (entity == null || entity.getStaticSystemConstantDetails() == null ||
                entity.getStaticSystemConstantDetails().size() <= 0) {
            return datas;
        }
        List<SearchMenuEntity.StaticSystemConstantDetailsBean> conditions = entity.getStaticSystemConstantDetails();
        for (SearchMenuEntity.StaticSystemConstantDetailsBean bean : conditions) {
            String icon = "";
            if (Constants.TYPE_LEVEL.equals(type)) {
                icon = bean.getConstantName();
            } else if (Constants.TYPE_NATURE.equals(type)) {
                icon = bean.getConstantName() ;
            }
            ConditionBean conditionBean = new ConditionBean(type, bean.getConstantCode(),
                    bean.getConstantName(), icon, false);
            datas.add(conditionBean);
        }
        return datas;
    }

    @Override
    public void doLoadFilterMenuData() {
        if (mView == null)
            return;

        mView.onRefreshIdustryMenuData(getIdustryMenuData());
        mView.onRefreshSuperviseMenuData(getSuperviseNatureMenuData(), getSuperviseGradeMenuData());
        mView.onRefreshSmartMenuUI(getIFirmSortType());

    }

    private List<ConditionBean> getSuperviseNatureMenuData() {
        SearchMenuEntity nature = QueryConfigManager.getInstance().getSuperviseNatureIdData();
        return CoverMenuData(nature, Constants.TYPE_NATURE);
    }

    private List<ConditionBean> getSuperviseGradeMenuData() {
        SearchMenuEntity superviseGradeIdData = QueryConfigManager.getInstance().getSuperviseGradeIdData();
        return CoverMenuData(superviseGradeIdData, Constants.TYPE_LEVEL);
    }


    private List<ConditionBean> getIdustryMenuData() {
        SearchMenuEntity industryType = QueryConfigManager.getInstance().getIndustryType();
        return CoverMenuData(industryType, Constants.TYPE_IDUSTRY);
    }

    private List<ConditionBean> getIFirmSortType() {
        SearchMenuEntity industryType = QueryConfigManager.getInstance().getFirmSortType();
        return CoverMenuData(industryType, Constants.TYPE_SMART_SORT);
    }


    @Override
    public void putSearTarget(String type, String value) {
        operateSearData(true, type, value);
    }

    @Override
    public void removeSearchData(String type, String value) {
        operateSearData(false, type, value);
    }

    private void operateSearData(boolean flag, String type, String value) {
        switch (type) {
            case Constants.TYPE_AREA://地区
                List listArea = (List) mQueryMap.get(Constants.TYPE_AREA);
                if (flag) {
                    listArea.add(value);
                } else {
                    listArea.remove(value);
                }
                break;
            case Constants.TYPE_IDUSTRY://行业类型
                List listIdustry = (List) mQueryMap.get(Constants.TYPE_IDUSTRY);
                if (flag) {
                    listIdustry.add(value);
                } else {
                    listIdustry.remove(value);
                }
                break;
            case Constants.TYPE_NATURE://监管
                if (flag) {
                    mQueryMap.put(Constants.TYPE_NATURE, value);
                } else {
                    mQueryMap.put(Constants.TYPE_NATURE, "");
                }
                break;
            case Constants.TYPE_LEVEL:
                if (flag) {
                    mQueryMap.put(Constants.TYPE_LEVEL, value);
                } else {
                    mQueryMap.put(Constants.TYPE_LEVEL, "");
                }
                break;
            case Constants.TYPE_SMART_SORT:
                if (flag) {
                    mQueryMap.put(Constants.TYPE_SMART_SORT, value);
                } else {
                    mQueryMap.put(Constants.TYPE_SMART_SORT, "");
                }
                break;
        }
    }

    @Override
    public void doSearchFirmList() {

    }

    @Override
    public void doLoadData() {
        startRefreshData();
    }

    @Override
    public void doLoadMoreData() {
        startLoadMoreData();

    }

    @Override
    public void doCleanCondition(String type) {
        switch (type) {
            case Constants.TYPE_IDUSTRY://行业类型
                List data = (List) mQueryMap.get(type);
                if (data != null) {
                    data.clear();
                }
                break;
            case Constants.TYPE_SUPERVISE://监管
                mQueryMap.put(Constants.TYPE_NATURE, "");
                mQueryMap.put(Constants.TYPE_LEVEL, "");
                break;
            case Constants.TYPE_SMART_SORT:
                mQueryMap.put(type, "");
                break;
        }
    }

    @Override
    public void loadData() {
        CURRENT_PAGE_SIZE = 0;
        netloadData(1);
    }

    @Override
    public void loadMoreData() {
        netloadData(CURRENT_PAGE_SIZE + 1);
    }


    /**
     * 网络拉取公司列表数据
     *
     * @param PAGE_Numb 拉取第几页的数据的页数
     */
    @SuppressWarnings("unchecked")
    private void netloadData(int PAGE_Numb) {
        String sType = (String) mQueryMap.get(Constants.TYPE_LEVEL);
        String sNature = (String) mQueryMap.get(Constants.TYPE_NATURE);
        String sSrmart = (String) mQueryMap.get(Constants.TYPE_SMART_SORT);
        List Level = new ArrayList();//监管等级
        if (!TextUtils.isEmpty(sType)) {
            Level.add(mQueryMap.get(Constants.TYPE_LEVEL));
        }
        List nature = new ArrayList();//监管性质
        if (!TextUtils.isEmpty(sNature)) {
            nature.add(mQueryMap.get(Constants.TYPE_NATURE));
        }
        FirmNetHelper.getFirmListInfo(getPageDataSize(), PAGE_Numb, "", "", (String)
                mQueryMap.get(Constants.TYPE_SMART_SORT), (List<String>) mQueryMap.get(Constants.TYPE_IDUSTRY), Level, nature, this);
    }

    @Override
    public void onDataLoaded(BaseEntity<List<FirmListEntity>> data, int reqId) {
        if (mView == null) {
            return;
        }
        mView.dimissLoading();
        if (data != null) {
            //加载成功，记录页数
            CURRENT_PAGE_SIZE++;
            loadDataComplete(data.getData());
        } else {
            LoadDataFailure();
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null) {
            return;
        }
        mView.dimissLoading();
        LoadDataFailure();
        mView.showEmptyView(strRes, true);
    }


    @Subscribe
    public void onEventThread(EventEntity event) {
        if (event.key.equals(String.valueOf(EventBusConstants.QUERY_CONFIG))) {
            //收到搜索配置文件加载完成的消息
            if (event.value.equals(EventBusConstants.SUCCEED)) {
                doLoadFilterMenuData();
            }
        }
    }


}
